/**
 * 
 */
package com.desitek.thunder.repository;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Dhiraj
 * 
 */
@Repository
public class GenericRepository<E extends Serializable> implements
		IGenericRepository<E> {
	@Autowired
	private SessionFactory sessionFactory;

	@SuppressWarnings("unchecked")
	public List<E> read(String query, Object[] params) {

		Query q = getSession().createQuery(query);
		for (int i = 0; i < params.length; i++) {
			q.setParameter(i + 1, params[i]);
		}
		return q.list();
	}

	public void persist(E entity) {
		getSession().persist(entity);
	}

	public void remove(E entity) {
		getSession().delete(entity);

	}

	protected Session getSession() {

		return sessionFactory.getCurrentSession();
	}

}
